package com.loongtech.bi.action.authManagement;

import com.loongtech.bi.entity.POJO.PojoRoleFunction;
import com.loongtech.bi.entity.POJO.PojoRoleFunctionHelper;
import com.loongtech.bi.entity.system.EntitySysRoleFunction;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysRoleFunctionManager;
import com.loongtech.bi.manager.system.SysUserProjectRoleManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: yuri
 * Description: 角色功能管理
 * Date: 16:03 2017/5/4
 */
@RestController
@Scope("prototype")
@RequestMapping("/role_function")
public class SysRoleFunctionAction implements Serializable {
    private static final long serialVersionUID = 1L;
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private SysUserProjectRoleManager sysUserProjectRoleManager;
    @Resource
    private SysRoleFunctionManager sysRoleFunctionManager;


    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> list(@RequestParam(value = "id") Integer roleId) {
        List<EntitySysRoleFunction> result;
        try {
            result = sysRoleFunctionManager.findByQuery("from EntitySysRoleFunction where roleId = ? order by id asc", roleId);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        List<PojoRoleFunctionHelper> list = new ArrayList<>();
        if (result != null && result.size() > 0) {
            for (EntitySysRoleFunction entity : result) {
                PojoRoleFunctionHelper pojo = new PojoRoleFunctionHelper();
                pojo.setFunctionId(entity.getFunctionId());
                pojo.setFilter1(entity.getFilter1());
                pojo.setFilterVal1(entity.getFilterVal1());
                pojo.setFilter2(entity.getFilter2());
                pojo.setFilterVal2(entity.getFilterVal2());
                pojo.setFilter3(entity.getFilter3());
                pojo.setFilterVal3(entity.getFilterVal3());
                list.add(pojo);
            }
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), list);
    }

    @RequestMapping(value = "modify.do", method = {RequestMethod.POST})
    public ResultData<String> add(@RequestBody PojoRoleFunction pojo) {
        try {
            // 先删除后增加
            sysRoleFunctionManager.excuteUpdateByHql("delete from EntitySysRoleFunction where roleId = ?", pojo.getRoleId());
            if (pojo.getFunctions() != null && pojo.getFunctions().size() > 0) {
                for (PojoRoleFunctionHelper pojoHelper : pojo.getFunctions()) {
                    EntitySysRoleFunction entity = new EntitySysRoleFunction();
                    entity.setRoleId(pojo.getRoleId());
                    entity.setFunctionId(pojoHelper.getFunctionId());
                    entity.setFilter1(pojoHelper.getFilter1());
                    entity.setFilterVal1(pojoHelper.getFilterVal1());
                    entity.setFilter2(pojoHelper.getFilter2());
                    entity.setFilterVal2(pojoHelper.getFilterVal2());
                    entity.setFilter3(pojoHelper.getFilter3());
                    entity.setFilterVal3(pojoHelper.getFilterVal3());
                    sysRoleFunctionManager.persist(entity);
                }
            }
            sysLogManager.addLog(session.getUserName(), "增加角色功能关联信息成功. ");
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "modifyfilter.do", method = {RequestMethod.POST, RequestMethod.POST})
    public ResultData<String> add(EntitySysRoleFunction e) {
        try {
            System.out.println(e);
            EntitySysRoleFunction entity = sysRoleFunctionManager.getByQuery("from EntitySysRoleFunction where roleId = ? and functionId = ?", e.getRoleId(), e.getFunctionId());
            if (entity != null) {
                entity.setFilter1(e.getFilter1());
                entity.setFilter2(e.getFilter2());
                entity.setFilter3(e.getFilter3());
                entity.setFilterVal1(e.getFilterVal1());
                entity.setFilterVal2(e.getFilterVal2());
                entity.setFilterVal3(e.getFilterVal3());
                sysRoleFunctionManager.merge(entity);
                sysLogManager.addLog(session.getUserName(), "filter修改成功!roleId=" + e.getRoleId());
                return ResultData.success();
            } else {
                sysLogManager.addLog(session.getUserName(), "修改失败,不存在该角色或功能页面");
                return new ResultData<>(RetCode.eFunctionOrRoleEmpty.getErrorcode(), RetCode.eFunctionOrRoleEmpty.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }
}